<?php
class admin{

	var $cTime;
	var $db;
	var $tpl;
	var $page;
	function admin()
	{
		global $db;
		global $config;
		global $tpl;
		
		$this -> cTime  = date("Y-m-d H:i:s");
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
	} 
	//===========================================
	function is_login($action)
	{
		global $buid,$config;
		if(!$buid||!isset($_COOKIE["USER"]))
		{
			header("Location: $config[weburl]/login.php");
			exit();
		}
		if(empty($_SESSION["STATU"]))
		{
			$this->db->query("select statu from ".ALLUSER." WHERE userid='$buid'");
			$re=$this->db->fetchRow();
			$_SESSION["STATU"]=$re['statu'];
			if(empty($_SESSION["STATU"]))
				$_SESSION["STATU"]=2;
		}
		
		if($_SESSION["STATU"]<=-1&&$_GET['type']!='access_dine'&&$action!='logout')
		{
			header("Location: $config[weburl]/main.php?action=msg&type=access_dine");
			exit();
		}
		
		if(!empty($_SESSION["STATU"])&&$_SESSION["STATU"]==1&&$action!="myshop"&&$action!="msg"&&$action!="logout"&&$action!="admin_personal")
		{
			header("Location: $config[weburl]/main.php?action=msg&type=active");
			exit();
		}
	}
	function msg($url,$str=NULL,$time=NULL)
	{
		$str=urlencode($str);
		$url=urlencode($url);
		header("Location:?action=msg&str=$str&menu=$_GET[menu]&url=$url&time=$time");
	}
	function who_view_myshop($uid)
	{
		$sql="select a.time,b.user,b.logo,b.userid from ".READREC." a , ".ALLUSER." b 
		where a.userid=b.userid and a.tid='$uid' and a.type='3'";
		$this->db->query($sql);
		$re=$this->db->getRows();
		return $re;
	}
	function check_access($type,$ext=NULL)
	{
		global $config,$buid;
		include($config['webroot']."/config/usergroup.php");
		$ifpay=$_SESSION["IFPAY"];
		$my_group=$group[$ifpay]['modeu'];
		
		if($type=="pro")
		{
			include($config['webroot'].'/module/product/api.php');
			$pro=new product_api();
			$nums=$pro->get_num_by_user($buid);
		}
		if($type=="pro_cat")
		{
			include($config['webroot'].'/module/product/api.php');
			$pro=new product_api();
			$nums=$pro->get_custom_cat_num_by_user($buid);
		}
		if($type=="offer")
			$sql="select count(id) as num from ".INFO." WHERE userid='$buid'";
		if($type=="news")
			$sql="select count(nid) as num from ".NEWSD." WHERE uid='$buid'";
		if($type=="album")
			$sql="select count(id) as num from ".ALBUM." WHERE userid='$buid'";
		if($type=="video")
			$sql="select count(video_id) as num from ".VIDEO." WHERE userid='$buid'";
		if($type=="link")
			$sql="select count(link_id) as num from ".ULINK." WHERE userid='$buid'";
		if($type=="hr")
			$sql="select count(id) as num from ".ZP." WHERE userid='$buid'";
		if($type=="album_cat")
			$sql="select count(id) as num from ".CUSTOM_CAT." WHERE userid='$buid' and type=6";
		if($type=="project")
			$sql="select count(id) as num from ".PROJECT." WHERE userid='$buid'";
		if($type=="subscribe")
			$sql="select count(id) as num from ".SUBSCRIBE." WHERE userid='$buid'";
		
		if(!empty($sql)||!empty($nums))
		{
			if(!empty($nums))
			{
				if($nums>=$my_group[$type])
				{
					msg("main.php?action=msg&type=access_dine".$ext);
					return false;
				}
			}
			else
			{
				$this->db->query($sql);
				$re=$this->db->fetchRow();
				if($re['num']>=$my_group[$type])
				{
					msg("main.php?action=msg&type=access_dine".$ext);
					return false;
				}
			}
		}
		/*如 temp,refresh.....*/
		elseif(!$my_group[$type])
		{
			msg("main.php?action=msg&type=access_dine".$ext);
			return false;
		}

	}
	
	function get_user_common_cat($id)
	{
		$cats=array();
		$sql="select id,common_cat from ".SHOPSETTING." where userid='$id'";
		$this->db->query($sql);
		$rec=$this->db->fetchRow();
		if(!empty($rec['id']))
		{
			$cat=explode(",",$rec['common_cat']);
			foreach($cat as $v)
			{
				$vc=array();
				if(!empty($v))
				{
					$vc[]=substr($v,0,4);
					if(strlen($v)>4)
						$vc[]=substr($v,0,6);
					if(strlen($v)>6)
						$vc[]=substr($v,0,8);
					if(strlen($v)>8)
						$vc[]=$v;
					$key=implode(",",$vc);
					$cats[$key]=$this->getProTypeName($key);
				}
			}
			return $cats;
		}
		else
			return array();
	}
	function getProTypeName($prod)
	{	
		global $db,$cat_table;
		if(!empty($prod))
		{
			$sql = "select cat from $cat_table where catid in($prod)";
			$db->query($sql);
			$fieldlist="";
			while($v=$db->fetchRow())
			{
				if($v["cat"]!="")
				$fieldlist.=$v["cat"]."->";
			}
			$fieldlist = trim($fieldlist,"->");
		}
		return $fieldlist;
	}
	function check_shop_statu($uid)
	{
		/***0,资料不完整，没有开能。1.正常开通。2.管理员强制关闭**/
		@include_once($config['webroot'].'/config/module_company_config.php');
		if($module_company_config['open_shop_guide']==1)
		{
			//---------------企业资料完整性
			$shop['user']=$this->check_myshop($uid);
			//---------------联系人信息完整性
			$sql="SELECT * FROM ".ALLUSER." WHERE userid='$uid'";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
			if(!$re["name"]||!$re["position"]||!$re["tel"]||!$re["email"]||!$re["mobile"]||!$re["city"]||!$re["province"])
				$shop['alluser']=0;
			else
				$shop['alluser']=1;
			//----------------工商信息完整性
			include_once("module/company/includes/bizinfo_class.php");
			$bizinfo=new bizinfo();
			$de=$bizinfo->get_detail($uid);
			$shop['biz']=$de['statu'];
			//----------------商情条数
			$sql="select count(id) as num from ".INFO." WHERE userid='$uid'";
			$this->db->query($sql);
			$shop['offernum']=$this->db->fetchField('num');
			//-----------------新闻条数
			$sql="select count(nid) as num from ".NEWSD." WHERE uid='$uid'";
			$this->db->query($sql);
			$shop['newnum']=$this->db->fetchField('num');
			//-----------------友情连接条数
			$sql="select count(link_id) as num from ".ULINK." WHERE userid='$uid'";
			$this->db->query($sql);
			$shop['linknum']=$this->db->fetchField('num');
			//-----------------
			if($shop['user']>=2&&$shop['alluser']&&$shop['biz']&&$shop['offernum']>=5&&$shop['newnum']>=5&&$shop['linknum']>=3)
			{
				$sql="update ".USER." set shop_statu=1 where userid='$uid';";
				$this->db->query($sql);
			}
			//-----------------
			return $shop;
		}
		else
			return 'close';	
	}
	function check_myshop($uid=NULL)
	{
		global $buid;
		if(!empty($uid))
			$buid=$uid;
			
		$sql="SELECT * FROM ".USER." WHERE userid='$buid'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($re)
		{
			$_SESSION['identity']=$re['identity']*1;
			$_SESSION['IFPAY']=$re['ifpay']*1;
			return 2;//在企业信息存在，企业级会员。
		}
		else
			return 1;//企业信息不存
	}
	
	function clear_user_shop_cache()
	{
		global $config,$buid;
		$dir=$config['webroot'].'/cache/shop/'.get_userdir($buid).'/';
		$handle = opendir($dir); 
		while ($filename = readdir($handle))
		{
			if(!is_dir($dir.$filename))
				@unlink($dir.$filename);
		}
	}
	
	function set_user_ifpay()
	{
		global $buid;
		$sql="update ".USER." SET ifpay='1' where userid='$buid'";
		$this->db->query($sql);
	}
	function get_personal_detail($id)
	{
		global $buid,$config;
		if(empty($id))
			$id=$buid;
		$sql="select * from ".ALLUSER." a left join ".COUNTRY." b on a.country=b.id WHERE userid='$id'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($config['language']=='cn')
			$re['country']=$re['cname'];
		else
			$re['country']=$re['ename'];
		return $re;
	}
	function delete_personal($id)
	{
		global $buid;
		$sql="delete from ".ALLUSER." WHERE pid='$buid' and userid='$id'";
		$this->db->query($sql);
	}
	function get_personal_list($id)
	{
		$sql="select * from ".ALLUSER." WHERE pid='$id' order by userid desc";
		$this->db->query($sql);
		$re=$this->db->getRows();
		return $re;
	}
	function add_personal()
	{
		global $config,$buid;
		$p=$_POST;
		$p['pas']=md5($p['pass']);
		$time=time();
		$ip=getip();
		$p['sex']=empty($p['sex'])?1:$p['sex'];
		
		$sql="select user from ".ALLUSER." where user='$p[user]'";
		$this->db->query($sql);
		$user=$this->db->fetchField('user');
		if(empty($user))
		{
			$sql="insert into ".ALLUSER." 
			(pid,user,password,name,email,tel,qq,province,city,sex,msn,skype,position,mobile,lastLoginTime,ip)
			value
			('$buid','$p[user]','$p[pas]','$p[name]','$p[email]','$p[tel]','$p[qq]','$p[province]','$p[city]',
			'$p[sex]','$p[msn]','$p[skype]','$p[position]','$p[mobile]','$time','$ip')";
			$re=$this->db->query($sql);
			return $re;
		}
	}
	
	function update_personal($uid)
	{
		global $config,$buid;$logo=NULL;$ssql=NULL;
		if(empty($uid))
			$uid=$buid;
		$_POST['province']=!empty($_POST['province1'])?$_POST['province1']:$_POST['province'];
		$_POST['city']=!empty($_POST['city1'])?$_POST['city1']:$_POST['city'];
		$_POST['sex']=empty($_POST['sex'])?1:$_POST['sex'];
		
		$sql="UPDATE ".ALLUSER." SET
		name='$_POST[name]',email='$_POST[email]',email2='$_POST[email2]',tel='$_POST[tel]',qq='$_POST[qq]',province='$_POST[province]',city='$_POST[city]',sex='$_POST[sex]',
		msn='$_POST[msn]',skype='$_POST[skype]',position='$_POST[position]',mobile='$_POST[mobile]',logo='$_POST[logo]'
		WHERE userid='$uid'";
		
		$re=$this->db->query($sql);
		return $re;
	}

	function add_domin($uid=NULL)
	{
		$sql="select userid from ".UDOMIN." where userid='$uid'";
		$this->db->query($sql);	 
		if ($this->db->num_rows()<=0)
		  $sql="insert into ".UDOMIN." (`domin`,`userid`,`time`) values ('".trim($_POST['domain'])."','$uid',".time().")";
		else
		  $sql="update ".UDOMIN." set domin='".trim($_POST['domain'])."',time=".time()." where userid='$uid'";
		$this->db->query($sql);
	}
	function get_myshop_domin($uid=NULL)
	{
		$sql="select domin from ".UDOMIN." where userid='$uid'";
		$this->db->query($sql);
		return $this->db->fetchField('domin');
	}

	function resetpass($buid)
	{
		$sql="SELECT password FROM ".ALLUSER." WHERE userid='$buid'";
		$this->db->query($sql);
		$re=$this->db->fetchRow();
		if($re['password']!=md5($_POST["oldpass"]))
			msg("main.php?action=setpassword&info=1&menu=user");
		elseif($_POST["newpass"]!=$_POST["renewpass"])
			msg("main.php?action=setpassword&info=2&menu=user");
		else
		{
			$sql="UPDATE ".ALLUSER." SET password='".md5($_POST['newpass'])."' WHERE userid='$buid'";
			$re=$this->db->query($sql);
			if($re)
				msg("main.php?action=setpassword&info=3&menu=user");
		}
	}
	function getComCat()
	{
		$this->db->query("SELECT catid,cat FROM ".COMPANYCAT." WHERE catid<=9999 order by nums asc");
		$re=$this->db->getRows();
		foreach($re as $key=>$v)
		{
			$s=$v["catid"]."00";
			$b=$v["catid"]."99";
			$this->db->query("SELECT * FROM ".COMPANYCAT." WHERE catid>=$s and catid<=$b ORDER BY nums ASC");
			$re[$key]['sub']=$this->db->getRows();
		}
		return $re;
	}
	/**
	 * $type 1产品，2产品,3资讯,4展会,5视频,6相册
	 */
	//======Brad=======================
	function update_custom_cat($type='')
	{
		global $buid;
		foreach($_POST['name'] as $key=>$v)
		{
			if(!empty($_POST['cid'][$key]))
			{
				$sql="update ".CUSTOM_CAT." set name='$v',nums='".$_POST['nums'][$key]."' where
				id='".$_POST['cid'][$key]."'";
				$re=$this->db->query($sql);
			}
			elseif(!empty($v))
			{
				$sql="insert into ".CUSTOM_CAT." (userid,name,type,nums) values 
				('$buid','$v','$type','".$_POST['nums'][$key]."')";
				$re=$this->db->query($sql);
			}
		}
		if(is_array($_POST['sname']))
		{
			foreach($_POST['sname'] as $key=>$v)
			{
				if(!empty($_POST['scid'][$key])&&!empty($_POST['sname'][$key]))
				{
					$sql="update ".CUSTOM_CAT." set name='".$_POST['sname'][$key]."',nums='".$_POST['snums'][$key]."' where
					id='".$_POST['scid'][$key]."'";
					$re=$this->db->query($sql);
				}
			}
		}
		if(is_array($_POST['pid']))
		{
			foreach($_POST['pid'] as $key=>$v)
			{
				if(!empty($v)&&!empty($_POST['addsname'][$key]))
				{
					$sql="insert into ".CUSTOM_CAT." (userid,pid,name,type,nums) values 
					('$buid','".$_POST['pid'][$key]."','".$_POST['addsname'][$key]."','$type','".$_POST['addsnums'][$key]."')";
					$re=$this->db->query($sql);
				}
			}
		}
	}
	
	function add_custom_cat($type="")
	{
		global $config,$buid;
		$sql="insert into ".CUSTOM_CAT." (userid,name,type) values ('$buid','$_POST[name]','$type')";
		$re=$this->db->query($sql);
		msg("main.php?action=m&m=product&s=admin_product_cat");
	}
	function get_custom_cat_list($type="", $single="")
	{
		global $config,$buid;
		if($single>0)
		{
			$sql="select * from ".CUSTOM_CAT." where id='$single' and type='$type' order by nums asc";
			$this->db->query($sql);
			$re=$this->db->fetchRow();
		}
		else
		{
			$sql="select * from ".CUSTOM_CAT." where userid='$buid' and type='$type' and pid=0 order by nums asc";
			$this->db->query($sql);
			$re=$this->db->getRows();
			for($i=0;$i<count($re);$i++)
			{
				$sql="select count(*) as custom_cat_count from ".CUSTOM_CAT_REL." where custom_cat_id=".$re[$i]['id'];
				$this->db->query($sql);
				$custom_rel_list=$this->db->fetchRow();
				$re[$i]["count"] = $custom_rel_list['custom_cat_count'];
				
				$sql="select * from ".CUSTOM_CAT." where pid='".$re[$i]['id']."' and pid!=0 order by nums asc";
				$this->db->query($sql);
				$re[$i]['subcat']=$this->db->getRows();
			}
		}
		return $re;
	}

	function edit_custom_cat($type="",$editid="")
	{
		global $config,$buid;
		$sql="update ".CUSTOM_CAT." set name='$_POST[name]' where id='$editid' and userid='$buid'";
		$re=$this->db->query($sql);
		msg("main.php?action=m&m=product&s=admin_product_cat");
	}

	function del_custom_cat($deid="")
	{
		global $config,$buid;
		$sql="delete from ".CUSTOM_CAT." where id='$deid' and userid='$buid'";
		$re=$this->db->query($sql);
		$sql="delete from ".CUSTOM_CAT_REL." where custom_cat_id='$deid'";
		$this->db->query($sql);
	}
	
	function getCatName($table)
	{
		$sql="select * from $table where catid<9999 order by nums asc";
		$this->db->query($sql);
		$re=$this->db->getRows();
		return $re;
	}

	//##产品封面
	function get_cover_img( $pic='' ){
		if( $pic=='' )
			return NULL;
		$pic = explode( ',',$pic );
		foreach( $pic as $v ){
			if(file_exists('uploadfile/product/'.$v))
				return $v;
		}
		return '';
	}

	//##==================产品商情图片保存===================
	function add_pro_pic( $copy=false )
	{
		if(!empty($_POST['pic']))
		{	
			if( !isset($save_path) )
				$save_path = implode( '/',explode( '-',date('y-m-d') ) ).'/';
			$add_pic = array();
			$pic_list = explode(',',$_POST['pic']);
			$dest = 'uploadfile/product/'."$save_path";
			if(!file_exists($dest))
				mkdirs($dest);
			foreach( $pic_list as $key=>$i )
			{
				$pic_id = "$i.jpg";
				if(file_exists("uploadfile/product/thumb/$pic_id"))
				{
					if($copy==true)
					{
						$add_pic[] = $save_path.$pic_id;
						@copy( "uploadfile/product/thumb/$pic_id",$dest.$pic_id );
						@copy( 'uploadfile/product/thumb/big_'.$pic_id,$dest.'big_'.$pic_id );						
					}
					else
					{
						$desc_id = $i.rand(1,100).".jpg";
						$add_pic[] = $save_path.$desc_id;
						@rename( "uploadfile/product/thumb/$pic_id",$dest.$desc_id );
						@rename( 'uploadfile/product/thumb/big_'.$pic_id,$dest.'big_'.$desc_id );
					}
				}
				else
					unset($pic_list[$key]);
			}
		}
		if($add_pic!=null)
				return implode( ',',$add_pic );
			else
				return '';
	}
	//=========图片更新==============
	function update_pro_pic($pro_pic)
	{
		$pro_pic = $pro_pic==''?array():explode( ',',$pro_pic );
		//删除图片
		if(!empty( $_POST['dele_pic'] )){
		   $del_pic = explode( ',',$_POST['dele_pic'] );
			if($del_pic!=null){
				foreach( $del_pic as $key=>$pic_id ){
					if(array_search( $pic_id,$pro_pic )>-1){
						unset($pro_pic[array_search( $pic_id,$pro_pic )]);
						@unlink( 'uploadfile/product/'."$pic_id" );
						@unlink( 'uploadfile/product/'.substr($pic_id,0,strrpos($pic_id,'/')+1).'big_'.substr($pic_id,strrpos($pic_id,'/')+1));
					}
				}
			}
		}
		//添加图片
		if(!empty($_POST['pic']))
		{	
			$new_pic = $this->add_pro_pic();
			$pro_pic = $new_pic==''?$pro_pic:array_merge( array_merge( $pro_pic,explode(',',$new_pic) ));
		}
		return $pro_pic;
	}

}
?>